package com.yeyks.app.function.rocketmq.consumer;

import cn.hutool.json.JSONUtil;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.yeyks.app.controller.websocket.c2b.C2bGrabTheOrderWebSocket;
import com.yeyks.commonReference.append.rocketmq.pojo.RocketMQWebsocketBasePojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * websocket推送消息监听
 */
@Component
@Slf4j
public class GrabTheOrderWebsocketMessageListener implements MessageListener {

    @Autowired
    private C2bGrabTheOrderWebSocket c2bGrabTheOrderWebSocket;

    @Override
    public Action consume(Message message, ConsumeContext context) {
        String body = null;
        try {
            body = new String(message.getBody(), "utf-8");
            log.error("rocket:websocket推送消息如下:message:{}", body);
            RocketMQWebsocketBasePojo rocketMQWebsocketBasePojo = JSONUtil.toBean(body, RocketMQWebsocketBasePojo.class);
            c2bGrabTheOrderWebSocket.sendMessageToUser(rocketMQWebsocketBasePojo.getUserId(), rocketMQWebsocketBasePojo.getBody());
            return Action.CommitMessage;
        } catch (Exception e) {
            log.error("rocket:websocket推送消息消费失败:message:{}", body);
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }

}
